package com.lineage.server.command.executor;

import com.lineage.server.Shutdown;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Shutdown implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Shutdown.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1Shutdown();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (pc == null)
        _log.warn("系统命令执行: " + cmdName + " " + arg + " 秒后关闭伺服器。"); 
      if (arg.equalsIgnoreCase("now")) {
        Shutdown.getInstance().startShutdown(pc, 5, true);
        return;
      } 
      if (arg.equalsIgnoreCase("abort")) {
        Shutdown.getInstance().abort(pc);
        return;
      } 
      int sec = Math.max(5, Integer.parseInt(arg));
      Shutdown.getInstance().startShutdown(pc, sec, true);
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
}
